import axios from "../../http/axios.js";
import {
  apiGetServiceTime
} from "../../http/server.js";
import {
  oneDateGetNextSevenDate,
  date2day,
  date2YYYYMMDD,
  date2sevenDates
} from "../../utils/util.js";
import {
  res2data
} from "../../assets/js/serviceTime.js";
Page({
  data: {
    timeContent: [{
        title: {
          active: true, //true选择中的状态,false未选择中的状态
          titleTime: "星期一 (2019/04/02)"
        },
        allTime: [{
            timeOne: "3:00 上门",
            state: 1 //1为未选择但是可以选择的，2为已选择， 3为未选择但是不可以进行选择
          },
          {
            timeOne: "4:00 上门",
            state: 1
          },
          {
            timeOne: "5:00 上门",
            state: 3
          },
          {
            timeOne: "5:00 上门",
            state: 1
          },
          {
            timeOne: "5:00 上门",
            state: 3
          }
        ]
      },
      {
        title: {
          active: false, //true选择中的状态,false未选择中的状态
          titleTime: "星期二 (2019/04/02)"
        },
        allTime: [{
          timeOne: "3:00 上门",
          state: 1
        }]
      },
      {
        title: {
          active: true, //true选择中的状态,false未选择中的状态
          titleTime: "星期一 (2019/04/02)"
        },
        allTime: [{
            timeOne: "3:00 上门",
            state: 1 //1为未选择但是可以选择的，2为已选择， 3为未选择但是不可以进行选择
          },
          {
            timeOne: "4:00 上门",
            state: 1
          },
          {
            timeOne: "5:00 上门",
            state: 3
          },
          {
            timeOne: "5:00 上门",
            state: 1
          },
          {
            timeOne: "5:00 上门",
            state: 3
          }
        ]
      },
      {
        title: {
          active: true, //true选择中的状态,false未选择中的状态
          titleTime: "星期一 (2019/04/02)"
        },
        allTime: [{
            timeOne: "3:00 上门",
            state: 1 //1为未选择但是可以选择的，2为已选择， 3为未选择但是不可以进行选择
          },
          {
            timeOne: "4:00 上门",
            state: 1
          },
          {
            timeOne: "5:00 上门",
            state: 3
          },
          {
            timeOne: "5:00 上门",
            state: 1
          },
          {
            timeOne: "5:00 上门",
            state: 3
          }
        ]
      },
    ]
  },

  onLoad: function(options) {
    this.getServiceTime();
  },

  onReady: function() {

  },

  onShow: function() {

  },
  //获取服务时间
  getServiceTime: function() {
    return;
    var type = 1; //排班性质 1上门 2院内
    var data = {
      startTime: oneDateGetNextSevenDate().date,
      endTime: oneDateGetNextSevenDate().nextDate
    }
    var daysArr = date2sevenDates();
    axios({
      url: apiGetServiceTime + `/${type}`,
      data: data
    }).then(res => {
      var timeContent = res2data(res);
      this.setData({
        timeContent: timeContent
      })
    })
  },
  //选择某一个时间
  hanleClickTimeOne: function(e) {
    var obj = e.detail;
    var item = obj.item;
    var index = obj.index;
    var item1 = obj.item1;
    var index1 = obj.index1;
    var timeContent = this.data.timeContent;
    if (item1.state == 3) {
      wx.showToast({
        title: "该时间没有服务排班,请您选择其他服务时间",
        icon: "none"
      })
      return;
    } else if (item1.state == 2) {
      return;
    } else {
      timeContent.forEach((timeItem, timeIndex) => {
        if (timeIndex == index) {
          timeItem.allTime.forEach((timeItem1, timeIndex1) => {
            if (timeIndex1 == index1) {
              if (timeItem1.state == 1) {
                timeItem1.state = 2;
              } else if (timeItem1.state == 2) {

              } else {
                wx.showToast({
                  title: "该服务时间预约人数已经达到上限,请您选择其他服务时间吧！",
                  icon: "none"
                })
              }
            } else {
              if (timeItem1.state == 1) {

              } else if (timeItem1.state == 2) {
                timeItem1.state = 1;
              } else {

              }
            }
          })
        } else {
          timeItem.title.active = false;
          timeItem.allTime.forEach((timeItem1, timeIndex1) => {
            if (timeItem1.state == 1) {

            } else if (timeItem1.state == 2) {
              timeItem1.state = 1;
            } else {

            }
          })
        }
      })
    }
    this.setData({
      timeContent: timeContent
    })
  },
  //点击切换总体的时间
  handleClickSwitch: function(e) {
    var obj = e.detail;
    var index = obj.index;
    var timeContent = this.data.timeContent;
    timeContent.forEach((item1, index1) => {
      if (index1 == index) {
        item1.title.active = !item1.title.active;
      }
    });
    this.setData({
      timeContent: timeContent
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})